package com.zygh.lz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.lz.entity.Account;
import com.zygh.lz.entity.Illegal;
import com.zygh.lz.service.AccountService;
import com.zygh.lz.service.impl.IllegalService;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.vo.ResultBean;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;

/**
 * @author wxh
 */
@RequestMapping("/illegal")
@RestController
@Slf4j
public class IllegalController {
    @Resource
    private IllegalService illegalService;

    @Resource
    private AccountService accountService;

    @PostMapping("/add")
    @ApiOperation("新增违法信息")
    public ResultBean add(Principal principal, @RequestBody Illegal illegal) {
        Account sysUser = accountService.getByUsername(principal.getName());
        illegal.setSysStaffId(sysUser.getSysStaffId());
        try {
            boolean save = illegalService.save(illegal);
            return ResultUtil.execOp(1, "插入");
        } catch (Exception exception) {
            exception.printStackTrace();
            return ResultUtil.setError(500, "插入失败，数据已存在", null);
        }

    }

    @PostMapping("/update")
    @ApiOperation("修改")
    public ResultBean update(Principal principal, @RequestBody Illegal illegal) {
        Account sysUser = accountService.getByUsername(principal.getName());
        illegal.setSysStaffId(sysUser.getSysStaffId());
        boolean b = illegalService.updateById(illegal);
        return ResultUtil.execOp(1, "修改");
    }

    @GetMapping("/find/page")
    @ApiOperation("分页查看")
    public ResultBean findPage(Principal principal, @ApiParam(required = true, name = "pageNum", value = "当前页码")
    @RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                               @ApiParam(required = true, name = "pageSize", value = "每页显示条数")
                               @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
                               @ApiParam(required = false, name = "illegalCode", value = "违法代码")
                               @RequestParam(required = false, value = "illegalCode") String illegalCode,
                               @ApiParam(required = false, name = "type", value = "违法类别")
                               @RequestParam(required = false, value = "type") String type) {
        Account sysUser = accountService.getByUsername(principal.getName());
        log.debug("违法代码请求用户：{}", sysUser);
        PageHelper.startPage(pageNum, pageSize);
        List<Illegal> list = illegalService.list(Wrappers.<Illegal>lambdaQuery().or()
                .like(illegalCode != null, Illegal::getIllegalCode, illegalCode)
                .eq(type != null, Illegal::getType, type)
                .eq(Illegal::getSysStaffId, sysUser.getSysStaffId())
                .orderByDesc(Illegal::getId));
        PageInfo<Illegal> page = new PageInfo<>(list);
        return ResultUtil.setOK("查询成功", page);
    }

    @GetMapping("/getType")
    @ApiOperation("查询所有违法类型")
    public ResultBean getType(Principal principal) {
        Account sysUser = accountService.getByUsername(principal.getName());
        QueryWrapper<Illegal> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("type").eq("sys_staff_id", sysUser.getSysStaffId()).groupBy("type");
        List<Illegal> list = illegalService.list(queryWrapper);
        return ResultUtil.setOK("查询成功", list);
    }

    @PostMapping("/delete")
    @ApiOperation("删除违法信息")
    public ResultBean delete(@RequestBody List<Integer> id) {
        boolean b = illegalService.removeByIds(id);
        return ResultUtil.setOK("删除成功", null);
    }
}
